package com.chuantou.yuba.ui;

import java.util.ArrayList;
import java.util.List;

import android.os.AsyncTask;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.toolbox.NetworkImageView;
import com.chuantou.yuba.R;
import com.chuantou.yuba.dao.MessageDao;
import com.chuantou.yuba.pojo.Message;
import com.chuantou.yuba.ui.widget.LoadingView;
import com.chuantou.yuba.ui.widget.LoadingView.Style;
import com.chuantou.yuba.util.BitmapBinder;
import com.chuantou.yuba.util.ViewHolder;

public class SystemMessageActivity extends BaseActionBarActivity {
	private ListView mListView;
	private LoadingView mLoadingView;
	private MessageAdapter mAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.system_message_list);
		setupViews();
		setTitle(R.string.system_message);
		loadMessages();
	}

	private void setupViews() {
		mListView = (ListView) findViewById(android.R.id.list);
		mLoadingView = (LoadingView) findViewById(android.R.id.empty);
		mListView.setEmptyView(mLoadingView);
		mAdapter = new MessageAdapter();
		mListView.setAdapter(mAdapter);
		registerForContextMenu(mListView);
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		if (v == mListView) {
			getMenuInflater().inflate(R.menu.menu_delete, menu);
		}
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		if (item.getItemId() == R.id.menu_delete) {
			mAdapter.remove(((AdapterContextMenuInfo) item.getMenuInfo()).position);
			mAdapter.notifyDataSetChanged();
			return true;
		}
		return super.onContextItemSelected(item);
	}

	private void loadMessages() {
		mLoadingView.setStyle(Style.LOADING, null);
		new AsyncTask<Void, Void, List<Message>>() {

			@Override
			protected List<Message> doInBackground(Void... params) {
				return MessageDao.readAllMessage(getApplicationContext());
			}

			protected void onPostExecute(java.util.List<Message> result) {
				mLoadingView.setStyle(Style.EMPTY, getString(R.string.no_content));
				mAdapter.add(result);
				mAdapter.notifyDataSetChanged();
			};
		}.execute();
	}

	class MessageAdapter extends BaseAdapter {
		private List<Message> mMessages = new ArrayList<Message>();

		public void add(List<Message> list) {
			if (list != null && !list.isEmpty()) {
				mMessages.addAll(list);
			}
		}

		public void remove(Message message) {
			mMessages.remove(message);
		}

		public void remove(int position) {
			mMessages.remove(position);
		}

		@Override
		public int getCount() {
			return mMessages.size();
		}

		@Override
		public Object getItem(int position) {
			return mMessages.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = View.inflate(getApplicationContext(), R.layout.row_received_message, null);
			}
			Message message = mMessages.get(position);
			TextView timeView = ViewHolder.get(convertView, R.id.timestamp);
			timeView.setVisibility(View.GONE);

			NetworkImageView avatarView = ViewHolder.get(convertView, R.id.iv_userhead);
			BitmapBinder.bindCircularNetImage(getApplicationContext(), avatarView, null, R.drawable.default_avatar_round_small);

			TextView messageView = ViewHolder.get(convertView, R.id.tv_chatcontent);
			registerForContextMenu(messageView);
			messageView.setText(message.getMessage());
			return convertView;
		}

	}

}
